package hl.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

import hl.pageModel.Coach;
import hl.pageModel.DataGrid;
import hl.pageModel.Json;
import hl.service.CoachService;

@Namespace("/")
@Action(value = "coachAction")
public class CoachAction extends BaseAction implements ModelDriven<Coach> {

	Coach coach = new Coach();

	private CoachService coachService;

	@Override
	public Coach getModel() {
		return coach;
	}

	public CoachService getcoachService() {
		return coachService;
	}

	@Autowired
	public void setcoachService(CoachService coachService) {
		this.coachService = coachService;
	}


	public void add() {
		Json j = new Json();
		try {
			coachService.save(coach);
			j.setSuccess(true);
			j.setMsg("增加成功");

		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void getDatagrid() {
		DataGrid d = coachService.getDatagrid(coach);
		super.writeJson(d);
	}

	public void edit() {
		Json j = new Json();
		try {
			coachService.edit(coach);
			j.setSuccess(true);
			j.setMsg("修改成功！");

		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void remove() {
		Json j = new Json();
		try {
			coachService.remove(coach);
			j.setSuccess(true);
			j.setMsg("删除成功！");

		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}
	
	public void getComboBox() {
		super.writeJson(coachService.getComboBox());
	}
}
